/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MobCatTree.java
 *
 * Created on May 3, 2009, 3:53:20 AM
 */
package dialogs;

import util.CategoriaNode;
import java.awt.Cursor;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import mobcatadmin.MobCatAdminView;
import persistencia.controller.jpa.JdbcController;
import persistencia.controller.jpa.RegistroJpaController;
import persistencia.entidade.Registro;

/**
 *
 * @author Novo usuário
 */
public class MobCatTree extends javax.swing.JPanel {

    private MobCatAdminView parent;
    private RegistroJpaController regJpa = new RegistroJpaController();

    /** Creates new form MobCatTree */
    public MobCatTree(MobCatAdminView parent) {
        initComponents();
        this.parent = (MobCatAdminView) parent;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        popupMenuTree = new javax.swing.JPopupMenu();
        menuNovaCategoria = new javax.swing.JMenuItem();
        menuEditarCategoria = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        menuRemoverCat = new javax.swing.JMenuItem();
        jScrollPane1 = new javax.swing.JScrollPane();
        tree = new javax.swing.JTree();

        popupMenuTree.setInvoker(this);
        popupMenuTree.setName("popupMenuTree"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(mobcatadmin.MobCatAdminApp.class).getContext().getResourceMap(MobCatTree.class);
        menuNovaCategoria.setText(resourceMap.getString("menuNovaCategoria.text")); // NOI18N
        menuNovaCategoria.setName("menuNovaCategoria"); // NOI18N
        menuNovaCategoria.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuNovaCategoriaActionPerformed(evt);
            }
        });
        popupMenuTree.add(menuNovaCategoria);

        menuEditarCategoria.setText(resourceMap.getString("menuEditarCategoria.text")); // NOI18N
        menuEditarCategoria.setActionCommand(resourceMap.getString("menuEditarCategoria.actionCommand")); // NOI18N
        menuEditarCategoria.setName("menuEditarCategoria"); // NOI18N
        menuEditarCategoria.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuEditarCategoriaActionPerformed(evt);
            }
        });
        popupMenuTree.add(menuEditarCategoria);

        jSeparator1.setName("jSeparator1"); // NOI18N
        popupMenuTree.add(jSeparator1);

        menuRemoverCat.setText(resourceMap.getString("menuRemoverCat.text")); // NOI18N
        menuRemoverCat.setName("menuRemoverCat"); // NOI18N
        menuRemoverCat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuRemoverCatActionPerformed(evt);
            }
        });
        popupMenuTree.add(menuRemoverCat);

        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tree.setModel(new MobCatTreeModel());
        tree.setName("tree"); // NOI18N
        tree.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mouseClick(evt);
            }
        });
        tree.addTreeExpansionListener(new javax.swing.event.TreeExpansionListener() {
            public void treeCollapsed(javax.swing.event.TreeExpansionEvent evt) {
            }
            public void treeExpanded(javax.swing.event.TreeExpansionEvent evt) {
                treeTreeExpanded(evt);
            }
        });
        jScrollPane1.setViewportView(tree);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 366, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 512, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 512, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void mouseClick(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mouseClick
        if (getTree().getSelectionPath() == null) {
            return;
        }
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) getTree().getSelectionPath().getLastPathComponent();
        if (evt.getButton() == 3) {

            if (node instanceof CategoriaNode || (((String) node.getUserObject()).equals(MobCatTreeModel.NODE_CATEGORIA))) {

                getPopupMenuTree().setLocation(evt.getLocationOnScreen());
                getPopupMenuTree().setVisible(true);

            }
        } else if (evt.getButton() == 1 && evt.getClickCount() == 2) {

            if (node instanceof CategoriaNode) {
                CategoriaNode catNode = (CategoriaNode) node;
                //if (catNode.getCategoria().getRegistroCollection() != null && catNode.getCategoria().getRegistroCollection().size() > 0) {
                Cursor c = getTree().getCursor();
                getTree().setCursor(new Cursor(Cursor.WAIT_CURSOR));
                TabelaRegistros regList = new TabelaRegistros(catNode.getCategoria(), parent, this);
                parent.getTabCategoria().addTab(catNode.getCategoria().getNome(), regList);
                parent.getTabCategoria().setSelectedComponent(regList);
                getTree().setCursor(c);
            //}
            } else if (((String) node.getUserObject()).indexOf(MobCatTreeModel.NODE_REGISTRO_SEM_CAT) > -1) {

                Cursor c = getTree().getCursor();
                getTree().setCursor(new Cursor(Cursor.WAIT_CURSOR));
                List<Registro> regList = regJpa.findRegistroEntitiesSemCategoria();
                if (regList.size() > 0) {
                    TabelaRegistros regtab = new TabelaRegistros(regList, parent);
                    parent.getTabCategoria().addTab(MobCatTreeModel.NODE_REGISTRO_SEM_CAT, regtab);
                    parent.getTabCategoria().setSelectedComponent(regtab);
                }
                getTree().setCursor(c);
            }
        }
    }//GEN-LAST:event_mouseClick

    public void menuNovoProjetoActionPerformed(java.awt.event.ActionEvent evt, JFrame parent) {

        /*String str = JOptionPane.showInputDialog(this, "Nome do projeto");
        if (str == null) {
            return;
        } else if (str.isEmpty()) {
            JOptionPane.showMessageDialog(parent, "Digite um nome para o projeto.");
            menuNovoProjetoActionPerformed(evt, parent);
        } else {*/
            //JdbcController.getInstance().setSchema(str);
            tree.setModel(new MobCatTreeModel());
       // }
    }

    public void menuExcluirProjetoActionPerformed(java.awt.event.ActionEvent evt, JFrame parent) {
        if (JdbcController.getInstance().getSchema() == null || JdbcController.getInstance().getSchema().isEmpty()) {
            JOptionPane.showMessageDialog(parent, "Selecione um projeto para ser deletado.", "Aviso", JOptionPane.NO_OPTION);
        } else {
            if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(parent, "Essa operação irá deletar todos\n os dados e configurações do projeto,\ndeseja continuar?", "Aviso", JOptionPane.YES_NO_OPTION)) {
                JdbcController.getInstance().deleteSchemaCurrent();
                tree.setModel(new MobCatTreeModel());
            }
        }
    }

    private void menuNovaCategoriaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuNovaCategoriaActionPerformed
        DefaultMutableTreeNode node = (javax.swing.tree.DefaultMutableTreeNode) getTree().getSelectionPath().getLastPathComponent();
        if (node == null) {
            return;
        }
        new CategoriaDlg(parent.getFrame(), true, (MobCatTreeModel) tree.getModel()).showInserir(node);    
    }//GEN-LAST:event_menuNovaCategoriaActionPerformed

    private void menuRemoverCatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuRemoverCatActionPerformed

        DefaultMutableTreeNode node = (javax.swing.tree.DefaultMutableTreeNode) getTree().getSelectionPath().getLastPathComponent();
        if (node == null) {
            return;
        }
        int opt = JOptionPane.showConfirmDialog(this, "Deseja remover a categoria e as sub-categorias?\n Obs.: Os registros não serão apagados.", "Aviso", JOptionPane.OK_CANCEL_OPTION);
        if (opt == JOptionPane.OK_OPTION) {
            if (node instanceof CategoriaNode) {
                ((MobCatTreeModel) getTree().getModel()).deletarCategoria((CategoriaNode) node);
            }
        }
    }//GEN-LAST:event_menuRemoverCatActionPerformed

    private void menuEditarCategoriaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuEditarCategoriaActionPerformed
        DefaultMutableTreeNode node = (javax.swing.tree.DefaultMutableTreeNode) getTree().getSelectionPath().getLastPathComponent();
        if (node == null) {
            return;
        }
        if (node instanceof CategoriaNode) {
            new CategoriaDlg(parent.getFrame(), true, (MobCatTreeModel) tree.getModel()).showEditar((CategoriaNode) node);
        }
    }//GEN-LAST:event_menuEditarCategoriaActionPerformed

    private void treeTreeExpanded(javax.swing.event.TreeExpansionEvent evt) {//GEN-FIRST:event_treeTreeExpanded
        ((MobCatTreeModel) tree.getModel()).treeTreeExpanded(evt);
    }//GEN-LAST:event_treeTreeExpanded

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JMenuItem menuEditarCategoria;
    private javax.swing.JMenuItem menuNovaCategoria;
    private javax.swing.JMenuItem menuRemoverCat;
    private javax.swing.JPopupMenu popupMenuTree;
    private javax.swing.JTree tree;
    // End of variables declaration//GEN-END:variables

    public JScrollPane getJScrollPane1() {
        return jScrollPane1;
    }

    public void setJScrollPane1(JScrollPane jScrollPane1) {
        this.jScrollPane1 = jScrollPane1;
    }

    public JSeparator getJSeparator1() {
        return jSeparator1;
    }

    public void setJSeparator1(JSeparator jSeparator1) {
        this.jSeparator1 = jSeparator1;
    }

    public JMenuItem getMenuNovaCategoria() {
        return menuNovaCategoria;
    }

    public void setMenuNovaCategoria(JMenuItem menuNovaCategoria) {
        this.menuNovaCategoria = menuNovaCategoria;
    }

    public JMenuItem getMenuRemoverCat() {
        return menuRemoverCat;
    }

    public void setMenuRemoverCat(JMenuItem menuRemoverCat) {
        this.menuRemoverCat = menuRemoverCat;
    }

    public JPopupMenu getPopupMenuTree() {
        return popupMenuTree;
    }

    public void setPopupMenuTree(JPopupMenu popupMenuTree) {
        this.popupMenuTree = popupMenuTree;
    }

    public JTree getTree() {
        return tree;
    }

    public void setTree(JTree tree) {
        this.tree = tree;
    }
}
